#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

ADMIN_NAME = "admin-daemon"
GITOSIS_CONFIG_PATH = "gitosis.conf"

class Repository:
    def __init__(self):
        self.owner = ""
        self.collaborators = []
        self.name = ""
        
    def print_(self):
        string = ""
        
        string += "[group " + self.name + "]\n" #header
        
        members = [self.owner] + self.collaborators
        if self.owner != ADMIN_NAME:
            members+=[ADMIN_NAME] # add admin-daemon for possibility of serverside control
            
        members =" ".join(members) # make members a string before insertion
        string += "members = " + members + "\n"
        
        string += "writable = " + self.name + "\n\n"
        
        return string
        
        

class Config:
    
    def __init__(self):
        adminrepo = Repository()
        adminrepo.owner = ADMIN_NAME
        adminrepo.name = "gitosis-admin"
        self.repositories = []
        self.repositories.append(adminrepo)
    
    def flush(self):
#        if os.path.isfile(GITOSIS_CONFIG_PATH):#trying to fix dublicating issue
#            os.remove(GITOSIS_CONFIG_PATH)
        with open(GITOSIS_CONFIG_PATH, mode="w") as file:
            
            file.write("[gitosis]\n\n") #header
            
            for r in self.repositories:
                file.write( r.print_() )
    
    def append(self, repo):
        self.repositories.append(repo)

#a = Config()                 
#r = Repository()             
#r.owner = "clarence"                          
#r.collaborators = ["duke","michael","scott"]    
#r.name = "clarence_awesome_repo"            
#a.append(r)                                   
#a.flush()         